import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:shared_preferences/shared_preferences.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: IntentHandler(),
    );
  }
}

class IntentHandler extends StatefulWidget {
  @override
  _IntentHandlerState createState() => _IntentHandlerState();
}

class _IntentHandlerState extends State<IntentHandler> {
  String data = '';
  String? action;
  final MethodChannel method =
      const MethodChannel('com.example/client_channel');
  final Future<SharedPreferences> _prefs = SharedPreferences.getInstance();
  final Map<int, Widget> _faceWidget = {};
  int? selectedFaceId;
  @override
  void initState() {
    super.initState();
  }

  void _writeFaceCount(int num) async {
    final prefs = await _prefs;
    prefs.setInt('faceCount', num);
  }

  Future<int> _getFaceCount() async {
    final prefs = await _prefs;
    int faceCount = prefs.getInt('faceCount') ?? 0;
    return faceCount;
  }

  void _addFaceId(int id) {
    final btn = ElevatedButton(
      onPressed: () {
        selectedFaceId = id;
        setState(() {});
      },
      style: ButtonStyle(
          textStyle: WidgetStatePropertyAll(TextStyle(
              color: id == selectedFaceId ? Colors.green : Colors.black))),
      child: Text(id.toString()),
    );

    _faceWidget[id] = btn;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          const SizedBox(height: 50),
          Text('data: $data'),
          ElevatedButton(
            onPressed: () async {
              final faceId = await _getFaceCount();
              final result =
                  await method.invokeMethod('recordFace', {"faceId": faceId.toString()});

              if (!_faceWidget.keys.contains(result.toString())) {
                data = "添加了人脸特征值，id为$result";
                _addFaceId(faceId);
                _writeFaceCount(faceId + 1);
                setState(() {});
              } else {
                data = "人脸特征值已存在，id为$result";
                setState(() {});
              }
            },
            child: const Text("人脸录入"),
          ),
          ElevatedButton(
            onPressed: () async {
              final result = await method.invokeMethod('recognizeFace');
              data = "人脸比对结束，识别人脸为$result";
              setState(() {});
            },
            child: const Text("人脸比对"),
          ),
          ElevatedButton(
            onPressed: selectedFaceId == null
                ? null
                : () async {
                    final result = await method
                        .invokeMethod('verifyFace', {'faceId': selectedFaceId});
                    data = "人脸鉴别结束，比对结果为$result";
                    setState(() {});
                  },
            child: selectedFaceId == null
                ? const Text("选择一个FaceId以进行人脸鉴别")
                : const Text("人脸鉴别"),
          ),
          Container(
            height: 1,
            color: Colors.grey,
            margin: const EdgeInsets.only(top: 20),
          ),
          if (_faceWidget.isNotEmpty) Text("已选择FaceId: $selectedFaceId"),
          ..._faceWidget.values,
        ],
      ),
    );
  }
}
